//
//  SettingViewController.m
//  yunyan
//
//  Created by haichuang on 2018/2/26.
//  Copyright © 2018年 haichuang. All rights reserved.
//

#import "SettingViewController.h"
#import "FeedBackViewController.h"
#import "AboutUsViewController.h"
#import "LoginViewController.h"
#import "MailboxMessageViewController.h"

#import "ReleaseResultsView.h"
#import "MainTabBarController.h"
#import <SDImageCache.h>
@interface SettingViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView * settingTab;

@property (nonatomic, strong) NSArray * itemArr1;
@property (nonatomic, strong) NSArray * itemArr2;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.settingTab = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
   [self customNaviItemTitle:@"系统设置" isLeft:YES];
    _itemArr1 = @[@"意见反馈",@"关于我们"];
     _itemArr2 = @[@"客服热线",@"清除缓存"];
    [self.view addSubview:self.settingTab];
    
    WeakSelf;
   
        [_settingTab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.equalTo (weakSelf.view);
            
        }];
    
    self.settingTab.delegate = self;
    self.settingTab.dataSource = self;
  //   self.settingTab.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
   
}

- (UIView *)createFootView{
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.settingTab.bounds.size.width, 100)];
    
    UIButton *logoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [logoutBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logoutBtn setTitle: @"退出登录" forState:UIControlStateNormal];
    logoutBtn.titleLabel.font = [UIFont systemFontOfSize:18.f];
    //  logoutBtn.titleLabel.textColor = [UIColor redColor];
    logoutBtn.layer.masksToBounds = YES;
    logoutBtn.layer.cornerRadius = 10.f;
    logoutBtn.layer.borderWidth = 0.5;
    logoutBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    logoutBtn.backgroundColor = RGBA(238, 81, 81, 1);
    
    [logoutBtn addTarget:self action:@selector(logoutButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:logoutBtn];
    
    [logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(footer.mas_bottom);
        make.left.equalTo (footer.mas_left).offset (10.f);
        make.right.equalTo (footer.mas_right).offset (-10.f);
        
        make.height.equalTo (@(50));
    }];
    
    return footer;
}
#pragma mark - tableview delegete;


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
  
    
    return 2;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 10.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
  
    if (section == 0) {
        return CGFLOAT_MIN;
    }
    return 100.0f;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        
      UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CELL"];
          
        }
    
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       cell.textLabel.textColor = RGBA(51, 51, 51, 1);
       cell.textLabel.font = [UIFont systemFontOfSize:16.f];
    
    if (indexPath.section == 0) {
        
      cell.textLabel.text = _itemArr1[indexPath.row ];
    }
    
    if (indexPath.section == 1) {
        
        if (indexPath.row == 0) {
            cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
            cell.detailTextLabel.text = @"085186679033";
            cell.detailTextLabel.textColor = RGBA(51, 51, 51, 1);
            cell.detailTextLabel.font = [UIFont systemFontOfSize:16.0f];
        }
        if (indexPath.row==1) {
            
            double tmpSize = [[SDImageCache sharedImageCache]getSize]/1024/1024;
            cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM",tmpSize];;
            cell.detailTextLabel.textColor = RGBA(51, 51, 51, 1);
            cell.detailTextLabel.font = [UIFont systemFontOfSize:16.0f];
        }
        
         cell.textLabel.text = _itemArr2[indexPath.row ];
       
    }
    

        return  cell;
        
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
  
    if (section == 0) {
        return nil;
    }
    
    return [self createFootView];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
            FeedBackViewController * feedBackVC = [[FeedBackViewController alloc]init];
            
            [self.navigationController pushViewController:feedBackVC animated:YES];
            
        }else if (indexPath.row == 1){
            
            AboutUsViewController  * aboutVC = [[AboutUsViewController alloc]init];
            
            [self.navigationController pushViewController:aboutVC animated:YES];
        }
    }else{
        
        if (indexPath.row == 0) {
            
            [UIAlertController showAlertToViewController:self withTitle:@"是否拨打号码" message:@"085186679033" confirmHandler:^(id UIAlertAction) {
                if ([UIApplication instancesRespondToSelector:@selector(canOpenURL:)])  {
                    
                    NSString *telNumber = [NSString stringWithFormat:@"tel:%@",@"085186679033"];
                    NSURL *aURL = [NSURL URLWithString: telNumber];
                    if ([[UIApplication sharedApplication] canOpenURL:aURL]) {
                        // 可以响应拨号
                        [[UIApplication sharedApplication] openURL:aURL];
                    }else {
                        // 无法响应拨号
                        [MBProgressHUD showMessag:@"无法打开\"电话\"程序" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
                    }
                }
            }];
            
        }else if (indexPath.row == 1){
            
//            [[SDImageCache sharedImageCache] clearDisk];
            [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
            SHuiMBhudTextTimeStr(@"清除成功！");
            [_settingTab reloadData];
//            ReleaseResultsView *clearView = [[ReleaseResultsView alloc]init];
//            [clearView initResultsViewWithClear:YES];
//            clearView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
//            [[UIApplication sharedApplication].keyWindow addSubview:clearView];
//
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [clearView removeFromSuperview];
//            });
        }
        
    }
    
  
    
    
}
- (void)logoutButtonAction
{
    //TODO: Logout logic;
    NSLog(@"退出");
//    [UIUtils showHudSuccessWithTint:@"退出成功"];
    USER_DEFAULTSet(@"0", @"user_id");
    SHuiMBhudTextTimeStr(@"退出成功");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
        MainTabBarController * root = [[MainTabBarController alloc]init];
        [UIApplication sharedApplication].keyWindow.rootViewController = root;
    });
   
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
